package bundle;

import java.util.*;
import java.util.Arrays;

public class BundleList {

	public HashMap<Long, String> list;

	public BundleList() {
		list = new HashMap<Long, String>();
	}

	public BundleList(String firstMsg) {
		list = new HashMap<Long, String>();
		list.put(System.currentTimeMillis(), firstMsg);
	}

	public synchronized void add(String msg) {
		list.put(System.currentTimeMillis(), msg);
	}

	public synchronized void remove(Long stamp) {
		list.remove(stamp);
	}

	public synchronized LinkedList<String> getMessages() {
		LinkedList<String> result = new LinkedList<String>();
		Long[] sortedList = list.keySet().toArray(new Long[list.keySet().size()]);
		Arrays.sort(sortedList); // We ensure that messages will be resent in
									// the chronological order.
		for (Long l : sortedList) {
			result.addLast(list.get(l));
		}
		list = new HashMap<Long, String>();
		return result;
	}

	public synchronized long size() {
		long result = 0;
		for (Long l : list.keySet()) {
			result += Long.SIZE / 8 + list.get(l).length();
		}
		return result;
	}
}
